package com.ruoyi.contract.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.contract.domain.ContractMain;
import com.ruoyi.contract.service.IContractMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.contract.mapper.InvoiceMainMapper;
import com.ruoyi.contract.domain.InvoiceMain;
import com.ruoyi.contract.service.IInvoiceMainService;

/**
 * 发票管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-25
 */
@Service
public class InvoiceMainServiceImpl implements IInvoiceMainService 
{
    @Autowired
    private InvoiceMainMapper invoiceMainMapper;
    @Autowired
    private IContractMainService contractMainService;

    /**
     * 查询发票管理
     * 
     * @param gid 发票管理ID
     * @return 发票管理
     */
    @Override
    public InvoiceMain selectInvoiceMainById(String gid)
    {
        return invoiceMainMapper.selectInvoiceMainById(gid);
    }

    /**
     * 查询发票管理列表
     * 
     * @param invoiceMain 发票管理
     * @return 发票管理
     */
    @Override
    public List<InvoiceMain> selectInvoiceMainList(InvoiceMain invoiceMain)
    {
        return invoiceMainMapper.selectInvoiceMainList(invoiceMain);
    }

    /**
     * 新增发票管理
     * 
     * @param invoiceMain 发票管理
     * @return 结果
     */
    @Override
    public int insertInvoiceMain(InvoiceMain invoiceMain)
    {
        invoiceMain.setCreateTime(DateUtils.getNowDate());
        return invoiceMainMapper.insertInvoiceMain(invoiceMain);
    }

    /**
     * 修改发票管理
     * 
     * @param invoiceMain 发票管理
     * @return 结果
     */
    @Override
    public int updateInvoiceMain(InvoiceMain invoiceMain)
    {
        invoiceMain.setUpdateTime(DateUtils.getNowDate());
        return invoiceMainMapper.updateInvoiceMain(invoiceMain);
    }

    /**
     * 批量删除发票管理
     * 
     * @param gids 需要删除的发票管理ID
     * @return 结果
     */
    @Override
    public int deleteInvoiceMainByIds(String[] gids)
    {
        return invoiceMainMapper.deleteInvoiceMainByIds(gids);
    }

    /**
     * 删除发票管理信息
     * 
     * @param gid 发票管理ID
     * @return 结果
     */
    @Override
    public int deleteInvoiceMainById(String gid)
    {
        return invoiceMainMapper.deleteInvoiceMainById(gid);
    }

    @Override
    public int approveInvoiceMainById(String gid) {
        InvoiceMain invoiceMain = invoiceMainMapper.selectInvoiceMainById(gid);
        if (invoiceMain.getState()!=0){
            throw new CustomException("发票编号："+invoiceMain.getCode()+"已被确认");
        }
        this.updateContractState(invoiceMain);
        invoiceMain.setUpdateTime(DateUtils.getNowDate());
        invoiceMain.setUpdateBy( SecurityUtils.getUsername());
        invoiceMain.setState(1);
        return invoiceMainMapper.updateInvoiceMain(invoiceMain);
    }

    private void updateContractState(InvoiceMain invoiceMain){
        String contractId = invoiceMain.getContractId();
        if(StringUtils.isEmpty(contractId)){
            return;
        }
        ContractMain contractMain = contractMainService.selectContractMainById(contractId);
        if (contractMain!=null){
            Integer typeFlag = contractMain.getTypeFlag()==null?0:contractMain.getTypeFlag();//1表示依据发票结算
            BigDecimal contractAmount = contractMain.getContractAmount();
            BigDecimal invoiceAmount = contractMain.getInvoiceAmount();
            invoiceAmount = invoiceAmount.add(invoiceMain.getInvoiceAmount());
            if(typeFlag==0){
                int compareVal = invoiceAmount.compareTo(contractAmount);   //1大0等-1小
                contractMain.setInvoiceState(compareVal+2);//0未开票，1开票中，2开票完成，3超额开票
            }else{
                contractMain.setInvoiceState(1);
            }
            contractMain.setInvoiceAmount(invoiceAmount);
            contractMainService.updateContractMain(contractMain);
        }
    }
}
